using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyLib
{
    //public class MyListA : MyList<Channel, int>
    //{
    //    public MyListA()
    //    {
    //    }

    //    public void Add(int id, Item item)
    //    {
    //        base.Add(id, item);
    //    }

    //    override public void Remove(int id)
    //    {
    //        base.Remove(id);
    //    }
    //}

    public class MyList<ItemType, KeyType> : IEnumerable where KeyType : IComparable
    {
        private SortedList items;
        //private ArrayList items;

        public MyList()
        {
            items = new SortedList();
        }

        public bool Contains(KeyType id)
        {
            return items.Contains(id);
        }

        public ICollection Keys
        {
            get
            {
                return items.Keys;
            }
        }

        public int Count
        {
            get
            {
                return items.Count;
            }
        }

        virtual public void Add(KeyType id, ItemType item)
        {
            items.Add(id, item);
        }

        virtual public void Remove(KeyType id)
        {
            items.Remove(id);
        }

        public void Clear()
        {
            items.Clear();
        }

        public ItemType this[KeyType id]
        {
            get
            {
                if (items.Contains(id))
                    return (ItemType)items[id];
                else
                    return default(ItemType);
                //throw new Exception("Invalid key. key=" + id.ToString());
            }
            set
            {
                if (items[id] == null)
                    items.Add(id, value);
                else
                    items[id] = value;
            }
        }

        //public ItemType GetByIndex(int index)
        //{
        //    return (ItemType)items.GetByIndex(index);
        //}

        public IEnumerator GetEnumerator()
        {
            return new MyListEnumerator<KeyType>(items);
        }
    }

    public class MyListEnumerator<KeyType> : IEnumerator where KeyType : IComparable
    {
        private SortedList items;
        private bool ready;
        private KeyType last_key;

        public MyListEnumerator(SortedList _items)
        {
            items = _items;
            ready = false;
        }

        public bool MoveNext()
        {
            if (items.Count == 0)
            {
                ready = false;
                return false;
            }
            if (!ready)
            {
                ready = true;
                last_key = (KeyType)items.GetKey(0);
                return true;
            }
            int index = items.IndexOfKey(last_key);
            if (index == -1)
            {
                foreach (DictionaryEntry item in items)
                {
                    if (((IComparable)item.Key).CompareTo(last_key) > 0)
                    {
                        last_key = (KeyType)item.Key;
                        return true;
                    }
                }
            }
            else
            {
                if (index < items.Count - 1)
                {
                    last_key = (KeyType)items.GetKey(index + 1);
                    return true;
                }
            }
            ready = false;
            return false;
        }

        public void Reset()
        {
            ready = false;
        }

        public object Current
        {
            get
            {
                try
                {
                    return items[last_key];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
}
